package servlet.navigate;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.netscenery.yang.model.Leaf;
import com.netscenery.yang.model.Module;
import com.netscenery.yang.model.Type;
import com.netscenery.yang.model.Typedef;
import com.netscenery.yang.model.Union;

import offer.GetDataSource;
import offer.GetNodeFromModule;
import offer.system.ModuleTypeTool;
import operate.OperateType;

public class GoToFindUnionTypeServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public GoToFindUnionTypeServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control","no-cache"); 
		PrintWriter out = response.getWriter();
		String baseType           = request.getParameter("baseType");
		String derivedType        = request.getParameter("derivedType");
		String opType             = request.getParameter("opType"); 
		//��ǰ·������Ҷ�ӽڵ��·��
		String pearent_nodeLevel  = request.getParameter("parent_nodeId");
		String[]  pearent_levels  = pearent_nodeLevel.split("\\\\");
		//������Դ
		Module       module       = GetDataSource.getDataRead(request);
		Union unionType = null;
		Typedef typedef = null;
		
		Leaf     parentNode       = (Leaf)GetNodeFromModule.getDataDefByLervels(module, pearent_levels);
		
		/**
		 * 1������leafҳ��
		 * 2������Typedefҳ��
		 */
		//1��parent_nodeId ����leafʱ
		if(null != parentNode){
			unionType = (Union)parentNode.getType();
		}else{
	    //2������Typedefҳ��
			String[] leafPath = new String[pearent_levels.length-1];
			for(int i = 0 ; i < leafPath.length ; i++){
				leafPath[i] = pearent_levels[i];
			}
			typedef   = (Typedef)GetNodeFromModule.getItemNode(module, leafPath);
			unionType = (Union)typedef.getType();;
		}
		//unionType = (Union)ModuleTypeTool.getType(module, pearent_levels);
		Type subType = null;
		//1����Ӳ���
		if(opType.equalsIgnoreCase("add")){
			if(null != parentNode){
				subType = parentNode.createType(baseType, "".equalsIgnoreCase(derivedType)?null:derivedType);
			}else{
				subType = typedef.createType(baseType, "".equalsIgnoreCase(derivedType)?null:derivedType);
			}
			
			unionType.addType(subType);
		}else{
			//2���༭����
			int i = 0;
			for(i = 0 ; i < unionType.getTypesNumber(); i++){
				if(("".equalsIgnoreCase(derivedType))&&
						(baseType.equalsIgnoreCase(unionType.getType(i).getName()))){
					subType = unionType.getType(i);
					break;
				}else if(derivedType.equalsIgnoreCase(unionType.getType(i).getName())){
					subType = unionType.getType(i);
					break;
				}
			}
			//3�����Ҳ���ֱ�����
			if(i == unionType.getTypesNumber()){
				if(null != parentNode){
					subType = parentNode.createType(baseType, "".equalsIgnoreCase(derivedType)?null:derivedType);
				}else{
					subType = typedef.createType(baseType, "".equalsIgnoreCase(derivedType)?null:derivedType);
				}
				unionType.addType(subType);
			}
		}
		
		//��ݳ־û�
		GetDataSource.getDataWrite(request, module);
		
		request.setAttribute("itemNode", subType);
		request.setAttribute("opType", OperateType.LOAD);
		//����Union��ʵ�� Type���͵Ľڵ�
		pearent_nodeLevel = pearent_nodeLevel +"\\"+unionType.getName()+"\\"+subType.getName();
		
		request.setAttribute("nodeId", pearent_nodeLevel);
		String url = "/items/"+ subType.getClass().getSimpleName() +".jsp";
		ServletContext sc=this.getServletContext();
		RequestDispatcher rd=sc.getRequestDispatcher(url);
		rd.forward(request, response);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
